<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24" v-loading="typeLoading">
        <div class="head-container">
          <el-input
            v-model="helpName"
            placeholder="请输入帮助名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="helpList"
            :props="helpProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="helpId"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          >
            <div class="custom-tree-node" slot-scope="{ node, data }">
              <div class="fwrap">{{ node.label }}</div>
              <div>
                <el-button
                  type="text"
                  size="mini"
                  v-hasPermi="['system:help:edit']"
                  @click.stop="handleUpdate(data)"
                >
                  编辑
                </el-button>
                <el-button
                  type="text"
                  size="mini"
                  v-hasPermi="['system:help:remove']"
                  @click.stop="handleDelete(data)"
                >
                  删除
                </el-button>
              </div>
            </div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="帮助名称" prop="helpName">
            <el-input
              v-model="queryParams.helpName"
              placeholder="请输入帮助名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >
              搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
              重置
            </el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:help:add']"
            >
              新增
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
        </el-row>

        <el-table v-loading="loading" border :data="dataList">
          <el-table-column label="帮助名称" align="center" prop="helpName" />
          <el-table-column label="内容" align="center" prop="content">
            <template slot-scope="scope">
              {{ scope.row.content | filterText }}
            </template>
          </el-table-column>
          <el-table-column label="排序" align="center" prop="sort" width="100" />
          <el-table-column label="操作" align="center" width="180">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:help:edit']"
              >
                修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:help:remove']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改帮助对话框 -->
    <el-dialog
      :title="form.helpId ? '编辑' : '新增'"
      :visible.sync="open"
      width="50%"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父级" prop="helpParentId">
          {{ form.helpId == 0 ? "顶级" : getHelpName }}
        </el-form-item>
        <el-form-item :label="`${form.type == 1 ? '类型' : '帮助'}名称`" prop="helpName">
          <el-input
            v-model="form.helpName"
            :placeholder="`请输入${form.type == 1 ? '类型' : '帮助'}名称`"
          />
        </el-form-item>
        <el-form-item label="类型" v-if="!form.helpId" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio :label="1">分类</el-radio>
            <el-radio :label="2">内容</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model.number="form.sort"
            type="number"
            placeholder="数字越小越靠前"
          />
        </el-form-item>
        <el-form-item v-if="form.type == 2" label="内容" prop="content">
          <Editors v-model="form.content" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHelp, getHelpData, delHelp, addHelp, updateHelp } from "@/api/system/help";
export default {
  name: "Help",
  data() {
    return {
      typeLoading: false, // 类型加载
      loading: false, // 遮罩层
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      helpList: [], // 帮助中心列表
      dataList: [], // 帮助数据列表
      helpName: null, // 过滤帮助名称
      helpId: 0, // 帮助id
      helpParentId: 0, // 父级id
      helpProps: {
        children: "children",
        label: "helpName",
      },
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        helpName: null,
      },
      form: {}, // 表单参数
      rules: {
        sort: [{ pattern: /^[1-9]\d*$/, message: "请输入正整数", trigger: "blur" }],
      }, // 表单校验
    };
  },
  created() {
    this.getHelpList();
  },
  computed: {
    getHelpName() {
      let helpId = this.helpId;
      let helpParentId = this.helpParentId;
      let form = this.form;
      let res = null;
      if (!helpParentId || helpId == 0) {
        res = "顶级";
      } else {
        res = this.getParentName(form.helpId ? helpParentId : helpId, this.helpList);
      }
      return res;
    },
  },
  watch: {
    // 根据名称筛选
    helpName(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    /** 查询帮助中心列表 */
    getHelpList() {
      this.typeLoading = true;
      listHelp()
        .then((res) => {
          this.helpList = res.data;
        })
        .finally(() => {
          this.typeLoading = false;
        });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.helpName.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.helpId = data.helpId;
      this.helpParentId = data.helpParentId;
      this.handleQuery();
    },
    // 获取分类下帮助列表
    getList() {
      this.loading = true;
      getHelpData(this.helpId, this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 递归查找名称
    getParentName(id, data = []) {
      let res = "";
      for (let i = 0; i < data.length; i++) {
        if (data[i].helpId == id) {
          res = data[i].helpName;
          break;
        } else {
          if (data[i].children && data[i].children.length > 0) {
            res = this.getParentName(id, data[i].children);
            if (res) break;
          }
        }
      }
      return res;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        helpId: null,
        helpName: null,
        helpParentId: 0,
        content: null,
        type: 1,
        sort: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = Object.assign({}, row);
      this.helpId = row.helpId;
      this.helpParentId = row.helpParentId;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.form);
          if (data.type == 1) data.content = null;
          if (data.helpParentId == 0 || data.helpParentId == null) {
            data.helpParentId = this.helpId;
          }
          if (data.helpId) {
            updateHelp(data).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              if (data.type == 1) {
                this.getHelpList();
              } else {
                this.helpId = this.helpParentId;
                this.getList();
              }
            });
          } else {
            addHelp(data).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              if (data.type == 1) {
                this.getHelpList();
              } else {
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除名为【" + row.helpName + "】的数据项？")
        .then(() => {
          delHelp([row.helpId]).then(() => {
            this.$modal.msgSuccess("删除成功");
            if (row.type == 1) {
              this.getHelpList();
            } else {
              this.getList();
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
  .fwrap {
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
